<?php
$title = "OnlineShop- Good";
$css = "item.css";
include_once 'common/top.php';
include_once '../Class/DB.php';
$db=new DB();

$pid=$_GET['id'];
if(!isset($pid))
{
	$pid=1;
}
//leave message
$leave_message=$_POST["board"];
$star=$_POST["star"];
if(isset($leave_message))
{
	//echo $leave_message;
	//exit();
	$db->insert_message($pid,$_SESSION["User"]["uid"],$leave_message,$star);
?>
<script language="JavaScript">
window.location.href="item.php?id=<?php echo $pid;?>";
</script>
<?php	
}

$product=$db->get_pid_product($pid);
if($product[0]==null)
{
	?>
<script language="JavaScript">
alert("Product doesn't exist!");
window.location.href="index.php";
</script>
<?php 
}
$messagePerPage=4;//message per page
$totalPage=ceil($db->get_pid_messageNum($pid)/$messagePerPage); //total page
$page=$_GET['page']; //current page
if (!isset($page)) {
 $page=1;
}
 if($page>$totalPage)
{
	$page--;
}
if($page<=0)
{
	$page=1;
} 
$startCount=($page-1)*$messagePerPage; 
$messages=$db->get_productId_message($pid,$startCount, $messagePerPage);
$product=$db->get_pid_product($pid);
$product[0]["introduction"]=str_replace("\r\n", "<br/>", $product[0]["introduction"]);
$product[0]["introduction"]=str_replace("\r", "<br/>", $product[0]["introduction"]);
$product[0]["introduction"]=str_replace("\n", "<br>", $product[0]["introduction"]);
$score=$db->get_pid_score($pid);
$score=number_format($score,1, '.', '');

?>
<script src="../js/item.js" type="text/javascript"></script>
<div id="product">
	<div id="product_top"></div>
	<div id="product_show">
		<div>
			<div id="image">
				<img alt="good" src="<?php if(file_exists("../Image/product_img/".$product[0]["pid"].".jpg")){echo "../Image/product_img/".$product[0]["pid"].".jpg";}else{echo "../Image/product_img/item_none.png";}?>">
			</div>
			<div id="about">
				<h4><?php echo $product[0]["p_name"];?></h4>
				<form action="cart.php" method="post">
				<input type="hidden" name="flag" id="id_flag" value="cart"/>
				<input type="hidden" name="item_pid" value="<?php echo $product[0]["pid"];?>"/>
					<table>
						<tr>
							<th>
								<p>Original Price:</p>
							</th>
							<td>
								<p class="no">$<?php echo $product[0]["ori_price"];?></p>
							</td>
						</tr>
						<tr>
							<th>
								<p>Current Price:</p>
							</th>
							<td>
								<p class="price">$<?php echo $product[0]["act_price"];?></p>
							</td>
						</tr>
						<tr style="height: 30px">
							<th>
								<p>Delivery Way:</p>
							</th>
							<td>
								<p>Express：$10.00 &nbsp;&nbsp; EMS：$10.00</p>
							</td>
						</tr>
						<tr style="height: 30px">
							<th>
								<p>Been Sold:</p>
							</th>
							<td>
								<p class="sold"><?php echo $db->get_pid_sold($product[0]["pid"]);?></p>
							</td>
						</tr>
						<tr style="height: 30px">
							<th>
								<p>Score:</p>
							</th>
							<td>
							<?php 
							$num_start=floor($score);
							$left=$score-$num_start;
							for($k=0;$k<$num_start;$k++)
							{
							?>
							<img src="../image/item_start.png">
							<?php 
							}
							if($left<0.5)
							{
							for($k=0;$k<5-$num_start;$k++)
							{
							?>
							<img src="../image/item_start_none.png">
							<?php 
								
							}
							}else//>=0.5
							{
							?>
							<img src="../image/item_start_half.png">
							<?php 
							for($k=0;$k<5-$num_start-1;$k++)
							{
							?>
							<img src="../image/item_start_none.png">
							<?php 	
							}
							}
							?>
								<p><?php echo $score;?></p>
								<p>(&nbsp;</p>
								<p class="sold"><?php echo $db->get_pid_messageNum($pid);?></p>
								<p>people have marked it)</p>
							</td>
						</tr>
						<tr style="height: 30px">
							<th>
								<p>Point:</p>
							</th>
							<td>
								<p class="green"><?php echo $product[0]["act_price"]*0.01;?></p>
							</td>
						</tr>
						
						<tr>
							<th colspan="2" class="line">
							</th>
						</tr>
						
						<tr>
							<th>
							</th>
							<td>
								<p>You can put it into design list for designing your room</p>
							</td>
						</tr>
						
						<tr>
							<th>
							</th>
							<td>
								<input id="addInList" type="button" value="" onclick="window.location.href='itemList.php?add_pid=<?php echo $product[0]["pid"];?>';"/>
							</td>
						</tr>
						
						<tr>
							<th>
							</th>
							<td>
								<br/>
								<br/>
								<p>Or You can put it into cart directly</p>
								<br/>
							</td>
						</tr>
						
						<tr>
							<th>
								<p>Count:</p>
							</th>
							<td>
								<input id="pCount" type="text" name="number" value="1" onkeyDown="onlyNum();" >
								<p>unit(s)</p>
								<p class="gray">(<?php echo $product[0]["count"];?> Left)</p>
							</td>
						</tr>
						<tr>
							<th>
							</th>
							<td>
								<input type="submit" value="" onclick="check(<?php echo $product[0]["count"];?>);">
							</td>
						</tr>
					</table>
				</form>
			</div>
			<div class="clearfix"></div>
		</div>
		
		<div id="introduce" class="block_mode">
			<h3>Description</h3>
			<div class="block_context">
				<p><?php echo $product[0]["introduction"];?></p>
			</div>
		</div>
		
		<div id="estimate" class="block_mode">
			<h3>Comments From Customers</h3>
			<?php 
			$i=0;
			while(isset($messages[$i]))
			{
				$user_info=$db->get_uid_info($messages[$i]["uid"]);
			?>
			<div class="block_context">
				<div class="estimate">
					<b> 
					<?php 
					        echo $user_info[0]["userName"];
					        $score=$messages[$i]["score"];
							$num_start=floor($score);
							$left=$score-$num_start;
							for($k=0;$k<$num_start;$k++)
							{
							?>
							<img src="../image/item_start.png">
							<?php 
							}
							if($left<0.5)
							{
							for($k=0;$k<5-$num_start;$k++)
							{
							?>
							<img src="../image/item_start_none.png">
							<?php 
								
							}
							}else//>=0.5
							{
							?>
							<img src="../image/item_start_half.png">
							<?php 
							for($k=0;$k<5-$num_start-1;$k++)
							{
							?>
							<img src="../image/item_start_none.png">
							<?php 	
							}
							}
							echo $messages[$i]["score"];
							?>
					</b>
					<p class="data"><?php echo substr($messages[$i]["date"],0,4)."-".substr($messages[$i]["date"],4,2)."-".substr($messages[$i]["date"],6,2)." ".substr($messages[$i]["date"],8,2).":".substr($messages[$i]["date"],10,2);?></p>
					<p><?php 
					$messages[$i]["content"]=str_replace("\r\n", "<br/>", $messages[$i]["content"]);
					$messages[$i]["content"]=str_replace("\r", "<br/>", $messages[$i]["content"]);
					$messages[$i]["content"]=str_replace("\n", "<br>", $messages[$i]["content"]);
					echo $messages[$i]["content"];
					?>
					</p>
				</div>
			</div>
			<?php 
			$i++;
			}
			?>
		<div id="page">
			<?php if ($page != 1) { ?>
			<a href="item.php?id=<?php echo $pid?>&page=<?php echo $page - 1;?>">Back</a> 
			<?php }
			for ($i=1;$i<=$totalPage;$i++) {  
				if($i==$page){
					?>
					<p><?php echo $i;?></p>
			<?php continue;}
			?>
			<a href="item.php?id=<?php echo $pid?>&page=<?php echo $i;?>"><?php echo $i ;?></a>
			<?php
			}
			if ($page<$totalPage) { 
			?>
				<a href="item.php?id=<?php echo $pid?>&page=<?php echo $page + 1;?>">Next</a>
			<?php
					} 
			?>
			
			</div>
		</div>
	<?php if($_SESSION["Passed"]){?>
		<div id="comments" class="block_mode">
			<h3>Your comments</h3>
			<div class="block_context">
				<div id="mark" >
					<div id="m0" class="star" 
					onclick="setMark(0);">
						<img src="../image/item_start_none.png">
					</div>
					<div id="m1" class="star" 
					onclick="setMark(1);">
						<img src="../image/item_start_none.png">
					</div>
					<div id="m2" class="star" 
					onclick="setMark(2);">
						<img src="../image/item_start_none.png">
					</div>
					<div id="m3" class="star" 
					onclick="setMark(3);">
						<img src="../image/item_start_none.png">
					</div>
					<div id="m4" class="star" 
					onclick="setMark(4);">
						<img src="../image/item_start_none.png">
					</div>
				</div>
				<form action="item.php?id=<?php echo $pid;?>" method="post" name="message_form">
				<textarea rows="" cols="" id="message_board" name="board"></textarea>
				<input type="submit" name="ok" value="Submit" onclick="checkMessage();">
				<input type="hidden" name="star" id="id_star" value="0" >
				</form>
			</div>
		</div>
		<?php }?>
	</div>
	
	
	<div id="product_end"></div>
</div>

<?php
include_once 'common/tail.php';
?>